﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using cfg = System.Configuration;
using GoldPrinter;
using WeifenLuo.WinFormsUI.Docking;
namespace BYManageInfoSystem
{
    public partial class Frm_SeaGateCompAddr : DockContent
    {
        private DGBY.BLL.BGHAddrTab m_bllBGHAddrTab = new DGBY.BLL.BGHAddrTab();
        private DGBY.BLL.BGHDataTab m_bllBGHDataTab = new DGBY.BLL.BGHDataTab();
        private DataSet m_ds = new DataSet();
        private int m_nSelectBGHAddrTabID = 0;
        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];

        private void Frm_SeaGateCompAddr_Load(object sender, EventArgs e)
        {
            DataSet ds = m_bllBGHDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxChooseSeaGateComperComp.DataSource = ds.Tables[0];
                this.comboBoxChooseSeaGateComperComp.DisplayMember = "报关行名称";
                this.comboBoxChooseSeaGateComperComp.ValueMember = "BGHDataTabID";
                this.comboBoxChooseSeaGateComperComp.SelectedIndex = 0;
            }
            if (Frm_Main.m_strLoginUser == "管理员")
            {
                this.buttonDel.Visible = true;
            }
        }

        private void comboBoxChooseSeaGateComperComp_SelectedIndexChanged(object sender, EventArgs e)
        {
            //try
            //{
            //    string strCompID = this.comboBoxChooseSeaGateComperComp.SelectedValue.ToString();
            //    dataGridViewSeaGateCompAddrList.DataSource = null;
            //    m_ds = m_bllBGHAddrTab.GetAddrListByCompID(strCompID);
            //    if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            //    {
            //        dataGridViewSeaGateCompAddrList.DataSource = m_ds.Tables[0];
            //        dataGridViewSeaGateCompAddrList.Columns[0].Visible = false;
            //    }
            //}
            //catch { }
        }

        public Frm_SeaGateCompAddr()
        {
            InitializeComponent();
        }

        private void ShowAllRecordInDataGridView()
        {
            dataGridViewSeaGateCompAddrList.DataSource = null;
            m_ds = m_bllBGHAddrTab.GetAllAddrList();
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dataGridViewSeaGateCompAddrList.DataSource = m_ds.Tables[0];
                dataGridViewSeaGateCompAddrList.Columns[0].Visible = false;
            }
        }

        private void buttonShowAllRecord_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }

        private void dataGridViewSeaGateCompAddrList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strBGHAddrTabID = this.dataGridViewSeaGateCompAddrList.Rows[this.dataGridViewSeaGateCompAddrList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectBGHAddrTabID = Convert.ToInt32(strBGHAddrTabID, 10);
            cellclickBGHAddrTab();
        }

        private void cellclickBGHAddrTab()
        {
            DGBY.Model.BGHAddrTab modelBGHAddrTab = m_bllBGHAddrTab.GetModel(m_nSelectBGHAddrTabID);
            if (modelBGHAddrTab != null)
            {
                if (modelBGHAddrTab.BGHDataTabID != null)
                    this.comboBoxChooseSeaGateComperComp.SelectedValue = modelBGHAddrTab.BGHDataTabID;
                this.textBoxSeaGateCompAddr.Text = modelBGHAddrTab.BGHAddr;
            }
            else
            {
                this.textBoxSeaGateCompAddr.Text = "";
            }
        }

        private void dataGridViewSeaGateCompAddrList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewSeaGateCompAddrList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }

        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.BGHAddrTab modelBGHAddrTab = new DGBY.Model.BGHAddrTab();
                modelBGHAddrTab.BGHAddr = this.textBoxSeaGateCompAddr.Text;

                if (this.comboBoxChooseSeaGateComperComp.SelectedValue != null)
                    modelBGHAddrTab.BGHDataTabID = Convert.ToInt32(this.comboBoxChooseSeaGateComperComp.SelectedValue.ToString(), 10);

                int nResult = m_bllBGHAddrTab.Add(modelBGHAddrTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加", this.comboBoxChooseSeaGateComperComp.Text + "(" + modelBGHAddrTab.BGHAddr + ")");

                    m_nSelectBGHAddrTabID = m_bllBGHAddrTab.GetMaxId() - 1;
                    ShowAllRecordInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_bllBGHAddrTab.GetModel(m_nSelectBGHAddrTabID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                DGBY.Model.BGHAddrTab modelBGHAddrTab = m_bllBGHAddrTab.GetModel(m_nSelectBGHAddrTabID);
                modelBGHAddrTab.BGHAddr = this.textBoxSeaGateCompAddr.Text;
              //  modelBGHAddrTab.BGHAddrTabID = m_nSelectBGHAddrTabID;

                if (this.comboBoxChooseSeaGateComperComp.SelectedValue != null)
                    modelBGHAddrTab.BGHDataTabID = Convert.ToInt32(this.comboBoxChooseSeaGateComperComp.SelectedValue.ToString(), 10);

                m_bllBGHAddrTab.Update(modelBGHAddrTab);

                Frm_Main.Log2DB(this.Text, "修改", this.comboBoxChooseSeaGateComperComp.Text + "(" + modelBGHAddrTab.BGHAddr + ")");
                ShowAllRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
         
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            if (m_bllBGHAddrTab.GetModel(m_nSelectBGHAddrTabID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllBGHAddrTab.Delete(m_nSelectBGHAddrTabID);
            ShowAllRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellclickBGHAddrTab();
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dataGridViewSeaGateCompAddrList.DataSource;
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    string templetFile = templetFilePath + "BGHAddr.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.DataTableToExcel(dt, 30, 6, 3, "页", 1);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("数据不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}